﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tireguit.Nmon.UI.Elements.Controls;
using log4net;
using System.Data.SqlClient;

namespace Tireguit.Nmon.UI
{
    public partial class MainConsole : Form
    {
        protected InformationTree _informationtree = null;
        protected PlotTree _plottree = null;
        protected PlotControl _plotcontrol = null;
        protected IDbConnection _connection=null;
        protected ILog _log=LogManager.GetLogger(typeof(MainConsole));
        protected BindingSource _datagridbindingSource = new BindingSource();
        protected string _selectednodename = string.Empty;

        protected string _connectionstring;
        public MainConsole()
        {
            InitializeComponent();
           // InitAll();
        }
        protected void HandleSelectTreeInformation(object o, TreeViewEventArgs e)
        {
            try
            {
                _log.Debug("start HandleSelectTreeInformation");
                _selectednodename = e.Node.Text;
                if (e.Node.Name == "GeneralInfo")
                {
                    _datagridbindingSource.DataSource = _informationtree.TreeElement.GetGeneralInformation();
                    dataGridView1.AutoResizeColumns( DataGridViewAutoSizeColumnsMode.AllCells);
                    return;
                }
                if (e.Node.Name=="DiskInfo")
                {
                    _datagridbindingSource.DataSource = _informationtree.TreeElement.GetDiskInformation();
                    dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                    return;

                }
                if (e.Node.Name == "DiskDetail")
                {
                    _datagridbindingSource.DataSource = _informationtree.TreeElement.GetDiskDetails();
                    dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                    dataGridView1.AutoResizeColumn(1, DataGridViewAutoSizeColumnMode.Fill);

                    return;
                }
                if (e.Node.Name == "AdaptInfo")
                {
                    _datagridbindingSource.DataSource = _informationtree.TreeElement.GetAdapterInformation();
                    dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                    return;
                }
                if (e.Node.Name.StartsWith("Cde"))
                {
                    _datagridbindingSource.DataSource = _informationtree.TreeElement.GetResCdes(e.Node.Text);
                    dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                    return;

                }
            }
            catch (System.Exception ex)
            {
                _log.Error("HandleSelectTreeInformation", ex);
            }
            finally
            {
                _log.Debug("stop HandleSelectTreeInformation");
            }
            

        }
        protected void Rafraichir()
        {
            try
            {
                _log.Debug("start Rafraichir");
                if (_informationtree!=null)
                {
                    splitContainer2.Panel1.Controls.Remove(_informationtree);
                    _informationtree.Dispose();
                    _informationtree = null;
                }
                if (_datagridbindingSource!=null)
                {
                    _datagridbindingSource.DataSource = null;
                }
                if (_plottree!=null)
                {
                    splitContainer3.Panel1.Controls.Remove(_plottree);
                    _plottree.Dispose();
                    _plottree = null;
                }
                if (_plotcontrol!=null)
                {
                    splitContainer3.Panel2.Controls.Remove(_plotcontrol);
                    _plotcontrol.Dispose();
                    _plotcontrol = null;
                }

                _informationtree = new InformationTree(_connection);
                _informationtree.Dock = DockStyle.Fill;
                _informationtree.Treeviewhandler = HandleSelectTreeInformation;
                splitContainer2.Panel1.Controls.Add(_informationtree);
                _plottree = new PlotTree(_connection);
                _plottree.Dock = DockStyle.Fill;
                splitContainer3.Panel1.Controls.Add(_plottree);
                _plotcontrol = new PlotControl(_connection, _plottree.TreeElement.StartDate,
                   _plottree.TreeElement.EndDate);
                _plotcontrol.Dock = DockStyle.Fill;
                splitContainer3.Panel2.Controls.Add(_plotcontrol);
                _plotcontrol.InitChar();
                _plottree.AddToGraph = _plotcontrol.AddSerie;
                _plottree.RemoveFromGraph = _plotcontrol.RemoveSerie;
            }
            catch (System.Exception ex)
            {
                _log.Error("Rafraichir", ex);
            }
            finally
            {
                _log.Debug("stop Rafraichir");
            }

        }
        protected void InitAll()
        {
            bool opened = false;
            try
            {
                _log.Debug("start InitALL");
                _connectionstring = "Server=(local)\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=Nmon.SqlDatabase";
               // _connection = new SqlConnection(_connectionstring);
                _connection = Tireguit.Nmon.CompactDb.DatabaseHelper.Instance.Connection;
                if (_connection.State!=ConnectionState.Open)
                {
                    _connection.Open();
                    opened = true;
                }

                Rafraichir();
                dataGridView1.DataSource = _datagridbindingSource;
                dataGridView1.Dock = DockStyle.Fill;
                dataGridView1.BackgroundColor = Color.White;
                dataGridView1.ContextMenuStrip = contextMenuStrip1;

            }
            catch (System.Exception ex)
            {
            	_log.Error("InitAll",ex);
                throw ex;
            }
            finally
            {
                if (opened)
                {
                    if (_connection.State!=ConnectionState.Closed)
                    {
                        _connection.Close();
                    }
                    
                }
                _log.Debug("stop InitALL");
            }

        }

        private void imprimerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tireguit.HelpPrint.PrintDGV printdgv = new Tireguit.HelpPrint.PrintDGV();
            //printdgv.Print_DataGridView(dataGridView1,false);
            printdgv.Print_DataGridView(dataGridView1, _selectednodename, true, true,DateTime.Now,true);

        }

        private void MainConsole_Load(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                InitAll();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Tireguit Nmon Analyser", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(1);
            	
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
            
        }

        private void aProposToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 aboutbox = new AboutBox1();
            aboutbox.ShowDialog();

        }

        private void imprimerToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.imprimerToolStripMenuItem_Click(sender, e);

        }

        private void supprimerTousLesGraphiquesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _plotcontrol.supprimerTousLesGraphsToolStripMenuItem_Click(sender, e);
        }

        private void imprimerLesGraphiquesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _plotcontrol.imprimerToolStripMenuItem_Click(sender, e);
        }

        private void légendesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _plotcontrol.afficherLégendeToolStripMenuItem_Click(sender, e);

        }

        private void chargerFichierToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            ParseForm parseform = new ParseForm(_connection);
            parseform.ShowDialog();
            Rafraichir();
            Cursor.Current = Cursors.Default;

        }

        private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }
    }
}
